package com.mingshashan.building.easy.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mingshashan.building.easy.core.common.EasyPage;
import com.mingshashan.building.easy.core.common.IMybatisConstants;
import com.mingshashan.building.easy.core.common.PageRequest;
import com.mingshashan.building.easy.core.dao.IPositionMapper;
import com.mingshashan.building.easy.core.entity.Position;
import com.mingshashan.building.easy.core.exception.EasyErrorCoded;
import com.mingshashan.building.easy.core.service.IPositionService;
import com.mingshashan.building.easy.core.vo.PositionCreationVO;
import com.mingshashan.building.easy.core.vo.PositionUpdateVO;
import com.mingshashan.building.easy.core.vo.PositionVO;
import com.mingshashan.building.easy.id.generator.IdGenerator;
import com.mingshashan.building.easy.log.Logger;
import com.mingshashan.building.easy.log.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * PositionServiceImpl
 *
 * @author jasonxu
 */
@Service
public class PositionServiceImpl implements IPositionService {

    private static final Logger logger = LoggerFactory.getLogger(PositionServiceImpl.class);

    @Autowired
    private IPositionMapper positionMapper;

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public PositionVO add(PositionCreationVO positionCreationVO) {

        Position position = modelMapper.map(positionCreationVO, Position.class);

        position.setId(idGenerator.nextId());

//        int result = positionMapper.insert(position);

        throw EasyErrorCoded.POSITION_CODE_EMPTY.exception();
//        if (IMybatisConstants.INSERT_RESULT != result) {
//
//        }
//
//        return modelMapper.map(position, PositionVO.class);
    }

    @Override
    public void delete(long id) {

        positionMapper.deleteById(id);
    }

    @Override
    public PositionVO update(long id, PositionUpdateVO positionUpdateVO) {

        // check if exist
        Position position = positionMapper.selectById(id);
        if (ObjectUtils.isEmpty(position)) {
            throw EasyErrorCoded.POSITION_NOT_EXIST_BY_ID.exception(id);
        }
        modelMapper.map(positionUpdateVO, position);
        int count = positionMapper.updateById(position);

        return modelMapper.map(position, PositionVO.class);
    }

    @Override
    public PositionVO getById(long id) {

        Position position = positionMapper.selectById(id);
        if (ObjectUtils.isEmpty(position)) {
            return null;
        }

        PositionVO positionVO = modelMapper.map(position, PositionVO.class);
        return positionVO;
    }

    @Override
    public PositionVO getByCode(String code) {

        if (StringUtils.isBlank(code)) {
            throw EasyErrorCoded.POSITION_CODE_EMPTY.exception(HttpStatus.BAD_REQUEST);
        }

        Position position = positionMapper.selectByCode(code);
        return null == position ? null : modelMapper.map(position, PositionVO.class);
    }

    @Override
    public EasyPage<PositionVO> searchPositions(String code, String name, PageRequest pageRequest) {

        IPage<Position> page = new EasyPage<>(pageRequest.getPage(), pageRequest.getSize());

        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(code)) {
            queryWrapper.like("code", "%" + code + "%");
        }
        if (!StringUtils.isBlank(name)) {
            queryWrapper.like("name", "%" + name + "%");
        }

        IPage<Position> positionEasyPage = positionMapper.selectPage(page, queryWrapper);

        IPage<PositionVO> result =
                positionEasyPage.convert(item -> modelMapper.map(item, PositionVO.class));

        return (EasyPage<PositionVO>) result;
    }
}
